package servlets;

import java.io.IOException;
import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import sessions.SkillManagerRemote;
import sessions.UserManagerRemote;
import entities.Skill;

/**
 * Servlet implementation class ManageSkills
 */
public class ManageSkills extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ManageSkills() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute("username");
		try {
			Hashtable<String, String> env = new Hashtable<String, String>();
			env.put(Context.INITIAL_CONTEXT_FACTORY,
					"org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL, "localhost:1099");
			InitialContext jndiContext = new InitialContext(env);
			Object ref = null;
			ref = jndiContext.lookup("UserManager/remote");
			UserManagerRemote ur = (UserManagerRemote) ref;
			List<Skill> declaredSkills = ur.fetchDeclaredSkills(username);
			session.setAttribute("declaredSkills", declaredSkills);
			ref = jndiContext.lookup("SkillManager/remote");
			SkillManagerRemote sr = (SkillManagerRemote) ref;
			List<Skill> allSkills = sr.fetchAllSkills();
			session.setAttribute("allSkills", allSkills);
			response.sendRedirect("manageSkills.jsp");
			return;
		} catch (NamingException e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
